﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

using TLL.Repository;
using TLL.Finance.BankSys;
using TLL.UTL;
using TLL.Finance.Currencys;
using TLL.Debug;

namespace TLL.Finance.BankSys
{
    public class MerchantRepository : XMLRepository<BankMerchant>
    {
        public MerchantRepository(string debugURL, string codingKey) : base("Bank\\Merchant.data", debugURL, codingKey, true, true, XMLDirectionType.Local) { }

        public override List<BankMerchant> Get(int id = 0)
        {
            try
            {
                var res = new List<BankMerchant>();

                foreach (XmlNode node in this.Document.ChildNodes[1])
                {
                    var currID = int.Parse(node.Attributes["ID"].Value);
                    res.Add(new BankMerchant(
                        this.AttributeByType<int>(node, "ID"),
                        this.AttributeByType<string>(node, "name"),
                        this.AttributeByType<string>(node, "cardNumber"),
                        this.AttributeByType<string>(node, "password"),
                        Utilities.GetEnum<CurrencyType>(node.Attributes["type"].Value)
                        ));
                }
                return res;
            }
            catch { this.Error = new Error(ErrorType.Get); }
            return null;
        }
        public override bool Create(BankMerchant item)
        {
            try
            {
                var newnode = this.Create_Node("merchant", this.Create_Attributes(new Dictionary<string, string>() {
                    { "ID", item.ID.ToString() },
                    { "name", item.Name },
                    { "cardNumber", item.Card },
                    { "password", item.Password },
                    { "type", item.CurrencyType.ToString() }
                }));
                this.Document.ChildNodes[1].AppendChild(newnode);
                return true;
            }
            catch 
            {
                this.Error = new Error(ErrorType.Create, new Dictionary<string, string>() { 
                    { "ID", item.ID.ToString() },
                    { "name", item.Name },
                    { "cardNumber", item.Card },
                    { "password", item.Password },
                    { "type", item.CurrencyType.ToString() }
                });
                this.Warning = new Warning(
                    WarningType.Create,
                    string.Format("Error creating merchant"),
                    this.Notify, 
                    delegate(object sender, EventArgs e) {
                        this.Error.Show(DisplayDebugType.MessageBox);
                    });
                return false; 
            }

        }
        public override bool Delete(int id)
        {
            try
            {
                foreach (XmlNode node in this.Document.ChildNodes[1].ChildNodes)
                    if (node.Attributes["ID"].Value == id.ToString())
                        this.Document.ChildNodes[1].RemoveChild(node);
                return true;
            }
            catch
            {
                this.Error = new Error(ErrorType.Delete, new Dictionary<string, string>() { 
                        { "ID", id.ToString() },
                });
                this.Warning = new Warning(
                    WarningType.Delete,
                    string.Format("Error deleting merchant"), 
                    this.Notify,     
                    delegate(object sender, EventArgs e) {
                        this.Error.Show(DisplayDebugType.MessageBox);
                    });
                return false;
            }
        }
    }
}
